package com.briup.test;

import com.briup.Dao.ClassMapper;
import com.briup.Dao.StuMapper;
import com.briup.pojo.Clazz;
import com.briup.pojo.Student;
import com.briup.uitls.SqlSessionFactoryUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Select {
    //查询所有的学生
    @Test
    public void Getall(){
        SqlSession sqlSession = null;
        try  {
            SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
            sqlSession   = sqlSessionFactory.openSession();
            StuMapper mapper = sqlSession.getMapper(StuMapper.class);
            List<Student> getall = mapper.getall();
            for (Student s : getall ) {
                System.out.println(s);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            sqlSession.close();
        }
    }
    //通过班级id查询一个班级的所有学生
    @Test
    public void getStudentByClassId(){
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);
        Clazz calzzAndStuById = mapper.getCalzzAndStuByClassId(1);
        System.out.println(calzzAndStuById);
        System.out.println(calzzAndStuById.getStus().toString());
        System.out.println();
        sqlSession.close();
    }
    //通过学生id查询学生信息和学生所在的班级，分步查询
    @Test
    public void getStuAndClassByIdStep(){
        SqlSession sqlSession = null;
        try  {
            SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
            sqlSession = sqlSessionFactory.openSession();
            StuMapper mapper = sqlSession.getMapper(StuMapper.class);
            Student stuByStep = mapper.getStuByStep(4);
            System.out.println(stuByStep);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            sqlSession.close();
        }
    }

    //通过id查询一条学生信息
    @Test
    public void getStudentById(){
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            Student student = sqlSession.selectOne("com.briup.Dao.StuMapper.getStudentById", "5");
            System.out.println(student);
        }finally{
            sqlSession.close();
        }
    }
    //通过Id获取一条学生信息
    @Test
    public void getStudentById1() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //获取 SqlSessionFactory 对象
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession sqlSession = sessionFactory.openSession();
        try{
            //获取接口的实现对象
            //会为接口创建一个代理对象
            StuMapper mapper = sqlSession.getMapper(StuMapper.class);
            //调用接口的方法
            Student studentById = mapper.getStudentById(7);
            System.out.println(studentById);
        }finally{
            sqlSession.close();
        }
    }
    //通过学生id查询学生信息和所在班级
    @Test
    public void getStuAndClass(){
        SqlSession sqlSession = null;
        try  {
            SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
            sqlSession = sqlSessionFactory.openSession(true);
            StuMapper mapper = sqlSession.getMapper(StuMapper.class);
            Student student = mapper.GetStuReturnStuAndClazz(4);
            System.out.println(student);

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            sqlSession.close();
        }
    }

    //通过班级Id查询班级的所有学生 分步查询
    @Test
    public void getStudentByClassIdStep(){
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);
        Clazz classAndStuByClassIdStep = mapper.getClassAndStuByClassIdStep(2);
    System.out.println(classAndStuByClassIdStep);
    System.out.println(classAndStuByClassIdStep.getStus().toString());
    }

    //discriminator鉴别器测试
    @Test
    public void getStudentByIdDis(){
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        Student studentByIdDis = mapper.getStudentByIdDis(5);
        System.out.println(studentByIdDis);
        sqlSession.close();
    }
}
